package com.coldchain.admin.config.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.coldchain.admin.config.entity.Area;
import com.coldchain.admin.config.entity.CarType;
import com.coldchain.admin.config.entity.CityInfo;
import com.coldchain.admin.config.entity.FeeScale;
import com.coldchain.admin.config.mapper.AreaMapper;
import com.coldchain.admin.config.service.IAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.admin.config.service.ICarTypeService;
import com.coldchain.admin.config.service.ICityInfoService;
import com.coldchain.admin.config.service.IFeeScaleService;
import com.coldchain.common.enums.EnableStatusEnums;
import com.fangliai.core.context.BaseContextHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fangliai
 * @since 2018-11-12
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {
    @Autowired
    private ICarTypeService carTypeService;

    @Autowired
    private IFeeScaleService feeScaleService;

    @Autowired
    private ICityInfoService citySetService;


    /**
     * 批量开放城市并设置收费标准
     *
     * @param params
     */
    @Override
    @Transactional
    public void openCity(String params) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(params);
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", jsonObject.getJSONArray("cityIds").toArray());
        List<Area> citys = super.list(queryWrapper);
        List<CityInfo> citySets=new ArrayList<>();
        List<CarType> carTypes = null;
        Boolean isMore = jsonObject.containsKey("feescal");
        if (isMore) {
            FeeScale oldfee = jsonObject.getJSONObject("feescal").toJavaObject(FeeScale.class);
            carTypes = carTypeService.list(null);
            List<FeeScale> feeScales = new ArrayList<>();
            for (Area v : citys) {
                citySets.add(new CityInfo(v.getId().toString(), EnableStatusEnums.OPEN));
                if (carTypes != null && !carTypes.isEmpty()) {
                    for (CarType cy : carTypes) {
                        FeeScale scale = oldfee.clone();
                        if (scale == null) {
                            scale = jsonObject.getJSONObject("feescal").toJavaObject(FeeScale.class);
                        }
                        scale.setCreateBy(BaseContextHandler.getUsername());
                        scale.setCreateTime(new Date().getTime());
                        scale.setAdecode(v.getId().toString());
                        scale.setCarTypeId(cy.getTypeId());
                        scale.setCreateTime(new Date().getTime());
                        scale.setCreateBy(BaseContextHandler.getUsername());
                        feeScales.add(scale);
                    }
                }

            }
            citySetService.saveBatch(citySets);
            if (!feeScales.isEmpty()) {
                feeScaleService.saveBatch(feeScales);
            }

        } else {
            for (Area c : citys) {
                citySets.add(new CityInfo(c.getId().toString(),EnableStatusEnums.OPEN));
            }
            citySetService.saveBatch(citySets);
        }

    }


    /**
     * 批量关闭城市
     *
     * @param ids
     * @throws Exception
     */
    @Override
    public void closeCity(Integer... ids) throws Exception {
        CityInfo cityset = new CityInfo();
        cityset.setEnable(EnableStatusEnums.CLOSE);
        citySetService.update(cityset, new UpdateWrapper<CityInfo>().in("id", Arrays.asList(ids)));
    }

    @Override
    public List<Map<String,Object>> selectCityVo(Map<String, Object> params) {
        return baseMapper.selectCityVo(params);
    }


}
